//app.js
var url = 'https://api.rootphp.com/'
var mdkplugin = require('./utils/mdk_plugin.js');
App({
  globalData: {
    userInfo: null,
    statusBarHeight: '',
    baseUrl: 'https://api.rootphp.com/',
    isIPX: false
  },
  onLaunch: function () {
    wx.removeStorageSync('onHomeLive')
    wx.getLaunchOptionsSync({
      success(res) {
        console.log(res)
      }
    })
    // 判断设备是否为 iPhone X
    this.checkIsIPhoneX()
    let systemInfo = wx.getSystemInfoSync()
    // px转换到rpx的比例
    let pxToRpxScale = 750 / systemInfo.windowWidth;
    var that = this
    wx.getSystemInfo({
      success: function (res) {
        that.globalData.statusBarHeight = res.statusBarHeight;
        that.globalData.clientHeight = res.windowHeight;
        console.log(res.statusBarHeight, 'statusBarHeight')
      }
    })
    if (wx.canIUse('getUpdateManager')) {
      const updateManager = wx.getUpdateManager()
      updateManager.onCheckForUpdate(function (res) {
        console.log('onCheckForUpdate====', res)
        // 请求完新版本信息的回调
        if (res.hasUpdate) {
          console.log('res.hasUpdate====')
          updateManager.onUpdateReady(function () {
            wx.showModal({
              title: '更新提示',
              content: '新版本已经准备好，是否重启应用？',
              success: function (res) {
                console.log('success====', res)
                // res: {errMsg: "showModal: ok", cancel: false, confirm: true}
                if (res.confirm) {
                  // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
                  updateManager.applyUpdate()
                }
              }
            })
          })
          updateManager.onUpdateFailed(function () {
            // 新的版本下载失败
            wx.showModal({
              title: '已经有新版本了哟~',
              content: '新版本已经上线啦~，请您删除当前小程序，重新搜索打开哟~'
            })
          })
        }
      })
    }
  },
  checkIsIPhoneX: function () {
    const self = this
    wx.getSystemInfo({
      success: function (res) {
        // 根据 model 进行判断
        if (res.model.search('iPhone X') != -1) {
          self.globalData.isIPX = true
        }
      }
    })
  },
  link: {
    url: url,
    wxLogin: 'v1/tbClassify'
  }
})